package com.yxsd.web.controller;

import com.pandawork.core.exception.SSException;
import com.pandawork.core.log.LogClerk;
import com.pandawork.core.util.Pagination;
import com.yxsd.common.annotation.IgnoreLogin;
import com.yxsd.common.annotation.Module;
import com.yxsd.common.entity.Order;
import com.yxsd.common.entity.Product;
import com.yxsd.common.entity.Supplier;
import com.yxsd.common.enums.ModuleEnums;
import com.yxsd.common.enums.OrderStatusEnums;
import com.yxsd.common.utils.WebConstants;
import com.yxsd.web.spring.AbstractController;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * IndexController
 *
 * @author: zhangteng
 * @time: 2015/1/4 16:55
 */
@IgnoreLogin
@Module(value = ModuleEnums.User)
@Controller
@RequestMapping(value = "")
public class IndexController extends AbstractController {

    @RequestMapping(value = "", method = RequestMethod.GET)
    public String index(Model model) {
        model.addAttribute("msg", "hello world!");
        return "index";
    }

    @Module(value = ModuleEnums.UserProductList)
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public String list(@RequestParam(value = "search", required = false) String search,
                       Model model) {
        Integer pageNo = 1;
        try {
            if (search == null || search.equals("") || search.length() == 0) {
                search = null;
            }
            int dataCount = productService.queryCountByCondition(search);
            Pagination page = new Pagination(dataCount, this.pageSize, pageNo);
            List<Product> productList = productService.queryByCondition(page, search);

            model.addAttribute("productList", productList);
            model.addAttribute("page", page);
            model.addAttribute("search", search);
            return "user_list";
        } catch (SSException e) {
            sendErrMsg(e.getMessage());
            return WebConstants.sysErrorCode;
        }
    }

    @Module(value = ModuleEnums.UserProduct)
    @RequestMapping(value = "{id}", method = RequestMethod.GET)
    public String toUpdate(@PathVariable("id") Integer id,
                           Model model) {
        try {
            Product product = productService.queryById(Product.class, id);
            Supplier supplier = supplierService.queryById(Supplier.class,product.getSupplierId());
            model.addAttribute("supplier", supplier);
            model.addAttribute("product", product);
        } catch (SSException e) {
            LogClerk.errLog.error(e);
            sendErrMsg(e.getMessage());
            return WebConstants.sysErrorCode;
        }
        return "user_view";
    }

    @Module(value = ModuleEnums.UserOrderNew)
    @RequestMapping(value = "", method = RequestMethod.POST)
    public String insert(Order order,Model model) {
        order.setStatus(OrderStatusEnums.Ordered.getId());
        try {
            order = orderService.insert(order);
            Product product = productService.queryById(Product.class, order.getProductId());

            product.setBorrowedNum(product.getBorrowedNum() + 1);
            productService.update(product);

            Supplier supplier = supplierService.queryById(Supplier.class,product.getSupplierId());
            model.addAttribute("supplier", supplier);
            model.addAttribute("product", product);
        } catch (SSException e) {
            LogClerk.errLog.error(e);
            sendErrMsg(e.getMessage());
            return WebConstants.sysErrorCode;
        }
        model.addAttribute("order",order);
        return "user_end";
    }

}
